package com.smarthome.common.tools;

import java.util.Random;
import java.util.UUID;

/**
 * 随时码工具类
 *
 */
public class RandomNoUtils {

    /**
     * 生成随机码
     * @param numberFlag 是否只是数字
     * @param length 随机码长度
     * @param numberLength 包含数字的长度(含字符串必传)
     * @return 随机码
     */

    public static String generateRandomNo(boolean numberFlag, int length, int numberLength) {
        String retStr = "";
        String strTable = numberFlag ? "1234567890" : "1234567890abcdefghijkmnpqrstuvwxyz";
        int len = strTable.length();
        // 设置跳出循环标志
        boolean bDone = true;
        do {
            retStr = "";
            int count = 0;
            for (int i = 0; i < length; i++) {
                // 产生一个O到length的随机数
                double dblR = Math.random() * len;
                int intR = (int) Math.floor(dblR);
                // 在基础码表中返回该随机数位置对应的值
                char c = strTable.charAt(intR);
                if (('0' <= c) && (c <= '9')) {
                    count++;
                }
                retStr += strTable.charAt(intR);
            }
            // 如果已生成了三个数字，标志置为false，跳出循环
            if (count >= numberLength) {
                bDone = false;
            }
        } while (bDone);
        return retStr;
    }

    public static String generateUUID() {
        return UUID.randomUUID().toString();
    }
    
    public static String genRandomNo(boolean numberFlag,int length){
    	String base = numberFlag ? "1234567890" : "abcdefghijklmnopqrstuvwxyz0123456789";     
	    Random random = new Random();     
	    StringBuilder sb = new StringBuilder();     
	    for (int i = 0; i < length; i++) {     
	        int number = random.nextInt(base.length());     
	        sb.append(base.charAt(number));     
	    }     
	    return sb.toString();    
    }
}
